package com.chiwen.system.service.impl;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.chiwen.common.core.domain.entity.SysDictData;
import com.chiwen.common.utils.DictUtils;
import com.chiwen.common.core.domain.entity.SysPayway;
import com.chiwen.common.utils.PayWayUtils;
import com.chiwen.common.utils.StringUtils;
import com.chiwen.system.mapper.SysPaywayMapper;
import com.chiwen.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.chiwen.system.service.ISysPaywayService;

import javax.annotation.PostConstruct;

/**
 * 支付通道Service业务层处理
 * 
 * @author Dipepr
 * @date 2022-11-13
 */
@Service
public class SysPaywayServiceImpl implements ISysPaywayService 
{
    @Autowired
    private SysPaywayMapper sysPaywayMapper;



    /**
     * 项目启动时，初始化字典到缓存
     */
    @PostConstruct
    public void init()
    {
        loadingSysPaywayCache();
    }

    /**
     * 加载字典缓存数据
     */
    @Override
    public void loadingSysPaywayCache()
    {

        Map<String, SysPayway> dictDataMap = new HashMap<>();
        List<SysPayway> sysPayways = sysPaywayMapper.selectSysPaywayList(new SysPayway());
        for (SysPayway item : sysPayways) {
            dictDataMap.put(item.getWayCode(),item);
        }

        for (Map.Entry<String, SysPayway> entry : dictDataMap.entrySet())
        {
            PayWayUtils.setDictCache(entry.getKey(), entry.getValue());
        }
    }

    /**
     * 查询支付通道
     * 
     * @param wayId 支付通道主键
     * @return 支付通道
     */
    @Override
    public SysPayway selectSysPaywayByWayId(Integer wayId)
    {
        return sysPaywayMapper.selectSysPaywayByWayId(wayId);
    }

    /**
     * 查询支付通道
     *
     * @param userId 支付通道主键
     * @return 支付通道
     */
    @Override
    public List<SysPayway> selectSysPaywayByUserId(Long userId)
    {
        return sysPaywayMapper.selectSysPaywayByUserId(userId);
    }


    /**
     * 查询支付通道
     *
     * @param wayCode 支付通道主键
     * @return 支付通道
     */
    @Override
    public SysPayway selectSysPaywayByWayCode(String wayCode)
    {

        SysPayway sysPayway = PayWayUtils.getDictCache(wayCode);
        if (sysPayway!=null){
            return sysPayway;
        }
        sysPayway = sysPaywayMapper.selectSysPaywayByWayCode(wayCode);
        if (sysPayway!=null){
            PayWayUtils.setDictCache(sysPayway.getWayCode(), sysPayway);
            return sysPayway;
        }
        return null;
    }


    /**
     * 查询支付通道列表
     * 
     * @param sysPayway 支付通道
     * @return 支付通道
     */
    @Override
    public List<SysPayway> selectSysPaywayList(SysPayway sysPayway)
    {
        return sysPaywayMapper.selectSysPaywayList(sysPayway);
    }

    /**
     * 新增支付通道
     * 
     * @param sysPayway 支付通道
     * @return 结果
     */
    @Override
    public int insertSysPayway(SysPayway sysPayway)
    {
        sysPayway.setCreateTime(DateUtils.getNowDate());

      int row= sysPaywayMapper.insertSysPayway(sysPayway);
        if (row > 0)
        {
            PayWayUtils.setDictCache(sysPayway.getWayCode(), sysPayway);
        }
        return row;
    }

    /**
     * 修改支付通道
     * 
     * @param sysPayway 支付通道
     * @return 结果
     */
    @Override
    public int updateSysPayway(SysPayway sysPayway)
    {
        sysPayway.setUpdateTime(DateUtils.getNowDate());
        int row= sysPaywayMapper.updateSysPayway(sysPayway);
        if (row > 0)
        {
            PayWayUtils.setDictCache(sysPayway.getWayCode(), sysPayway);
        }
        return row;
    }

    /**
     * 批量删除支付通道
     * 
     * @param wayIds 需要删除的支付通道主键
     * @return 结果
     */
    @Override
    public int deleteSysPaywayByWayIds(Integer[] wayIds)
    {
        int i =0;
        for (Integer wayId : wayIds) {
            SysPayway sysPayway = sysPaywayMapper.selectSysPaywayByWayId(wayId);

              i = deleteSysPaywayByWayId(wayId);
            if(i>0){
                DictUtils.removeDictCache(sysPayway.getWayCode());
            }

        }
        return i;
    }

    /**
     * 删除支付通道信息
     * 
     * @param wayId 支付通道主键
     * @return 结果
     */
    @Override
    public int deleteSysPaywayByWayId(Integer wayId)
    {
        return sysPaywayMapper.deleteSysPaywayByWayId(wayId);
    }
}
